﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex.Communication.Net.BMQ
{
    /// <summary>
    /// BMQ心跳-响应消息
    /// </summary>
    internal class BMQHeartResMessage : BMQSessionMessageAbs
    {
        /****************************************************
         * BMQ心跳-响应消息结构
         * 字段             数据类型    大小(byte)  说明
         * Type             byte        1           消息类型
         * ConttexId        Int16       2           上下文会话标识
         * CreateTimestamp  Int64       8           心跳创建时间戳
         * ResTimestamp     Int64       8           心跳回复时间戳
         ****************************************************/

        /// <summary>
        /// 消息大小
        /// </summary>
        private const int SIZE = 19;

        private long _createTimestamp;
        /// <summary>
        /// 心跳创建时间戳
        /// </summary>
        public long CreateTimestamp
        {
            get { return _createTimestamp; }
        }

        private long _resTimestamp;
        /// <summary>
        /// 心跳回复时间戳
        /// </summary>
        public long ResTimestamp
        {
            get { return _resTimestamp; }
        }


        /// <summary>
        /// 构造函数-解析
        /// </summary>
        public BMQHeartResMessage()
            : base(BMQMessageTypes.HeartRes, SIZE)
        {

        }


        /// <summary>
        /// 构造函数-创建
        /// </summary>
        /// <param name="contextId">请求传话标识</param>
        /// <param name="createTimestamp">心跳创建时间戳</param>
        /// <param name="resTimestamp">心跳回复时间戳</param>
        public BMQHeartResMessage(short contextId, long createTimestamp, long resTimestamp)
            : base(BMQMessageTypes.HeartRes, SIZE, contextId)
        {
            this._createTimestamp = createTimestamp;
            this._resTimestamp = resTimestamp;
        }



        /// <summary>
        /// 将消息写入二进制buffer
        /// </summary>
        /// <param name="writer">消息写器</param>
        protected override void WriteBytes(BinaryWriter writer)
        {
            writer.Write(this._createTimestamp);
            writer.Write(this._resTimestamp);
        }

        /// <summary>
        /// 读取消息到对象
        /// </summary>
        /// <param name="reader">消息读取器</param>
        protected override void PrimitiveFromBytes(BinaryReader reader)
        {
            this._createTimestamp = reader.ReadInt64();
            this._resTimestamp = reader.ReadInt64();
        }
    }
}
